# -------------------------------------------------------------------
# Purpose: Creates Table B44
# Author:  Max Posch, 25/07/2025
# Usage:   Source this script to generate the table.
# -------------------------------------------------------------------
# Check that required paths exist
stopifnot(dir.exists(pdataconfanalysis))
stopifnot(dir.exists(poutputappendix))

# Load data
ivAncestralCountryStocks <- fread(file.path(pdataconfanalysis, "ivAncestralCountryStocksMetaphone1900BoundariesPanel18951940.csv"))
ivAncestralCountryStocks <- ivAncestralCountryStocks[year >= 1900]


# Panel regressions for each year
o <- list()
vars1 <- c("iv_lo_adjp_t1880", "iv_lo_adjp_t1895_r")
o <- append(o, list(feols(n_coo_mp_adjp ~ .[vars1], ivAncestralCountryStocks[year == 1900])))

vars2 <- c("I_d_to_I_adjp_t1880_r", "I_d_to_I_adjp_t1895_r")
o <- append(o, list(feols(n_coo_mp_adjp ~ .[vars1] + .[vars2] | gisjoin_1900_neighb_coo_mp + coo_mp_region, ivAncestralCountryStocks[year == 1900])))

years <- c(1905, 1910, 1915, 1920, 1925, 1930)
for (y in years) {
  vars1 <- c(vars1, sprintf("iv_lo_adjp_t%d_r", y - 5))
  o <- append(o, list(feols(n_coo_mp_adjp ~ .[vars1], ivAncestralCountryStocks[year == y])))
  vars2 <- c(vars2, sprintf("I_d_to_I_adjp_t%d_r", y - 5))
  o <- append(o, list(feols(n_coo_mp_adjp ~ .[vars1] + .[vars2] | gisjoin_1900_neighb_coo_mp + coo_mp_region, ivAncestralCountryStocks[year == y])))
}

vars1 <- c(vars1, c("iv_lo_adjp_t1930_r"))
o <- append(o, list(feols(n_coo_mp_adjp ~ .[vars1], ivAncestralCountryStocks[year == 1940])))

vars2 <- c(vars2, "I_d_to_I_adjp_t1930_r")
o <- append(o, list(feols(n_coo_mp_adjp ~ .[vars1] + .[vars2] | gisjoin_1900_neighb_coo_mp + coo_mp_region, ivAncestralCountryStocks[year == 1940])))


# F-statistics
f <- lapply(o, function(x) wald(x, keep = "iv_lo_adjp", vcov = ~coo_mp))
fstat <- as.character(round(unlist(lapply(f, function(x) x$stat))))


# Create table
setFixest_dict(c(
  n_coo_mp_adjp = "No. of people in county $i$ with ancestral country $o$ in year:",
  iv_lo_adjp_t1880 = "$I_{o, -r(i)}^{1880} \\times \\frac{I_{-c(o), i}^{1880}}{I_{-c(o)}^{1880}}$",
  iv_lo_adjp_t1895_r = "$I_{o, -r(i)}^{1895} \\times \\frac{I_{-c(o), i}^{1895}}{I_{-c(o)}^{1895}}$",
  iv_lo_adjp_t1900_r = "$I_{o, -r(i)}^{1900} \\times \\frac{I_{-c(o), i}^{1900}}{I_{-c(o)}^{1900}}$",
  iv_lo_adjp_t1905_r = "$I_{o, -r(i)}^{1905} \\times \\frac{I_{-c(o), i}^{1905}}{I_{-c(o)}^{1905}}$",
  iv_lo_adjp_t1910_r = "$I_{o, -r(i)}^{1910} \\times \\frac{I_{-c(o), i}^{1910}}{I_{-c(o)}^{1910}}$",
  iv_lo_adjp_t1915_r = "$I_{o, -r(i)}^{1915} \\times \\frac{I_{-c(o), i}^{1915}}{I_{-c(o)}^{1915}}$",
  iv_lo_adjp_t1920_r = "$I_{o, -r(i)}^{1920} \\times \\frac{I_{-c(o), i}^{1920}}{I_{-c(o)}^{1920}}$",
  iv_lo_adjp_t1925_r = "$I_{o, -r(i)}^{1925} \\times \\frac{I_{-c(o), i}^{1925}}{I_{-c(o)}^{1925}}$",
  iv_lo_adjp_t1930_r = "$I_{o, -r(i)}^{1930} \\times \\frac{I_{-c(o), i}^{1930}}{I_{-c(o)}^{1930}}$",
  gisjoin_1900_neighb_coo_mp = "Neighboring ancestral countries-County", coo_mp_region = "Ancestral country-Census division"
))

tablename <- file.path(poutputappendix, "tableB44.tex")
etable(o,
  cluster = ~coo_mp,
  file = tablename, replace = TRUE,
  headers = rep(c("1900", "1905", "1910", "1915", "1920", "1925", "1930", "1940"), each = 2),
  keep = "!Intercept",
  fitstat = ~ n + r2, digits = "r3", digits.stats = 3,
  group = list("-_$I_{i}^{\\tau} / I^{\\tau}$ controls" = c("I_d_to_I_adjp_t1880_r", "I_d_to_I_adjp_t1895_r", "I_d_to_I_adjp_t1900_r", "I_d_to_I_adjp_t1905_r", "I_d_to_I_adjp_t1910_r", "I_d_to_I_adjp_t1915_r", "I_d_to_I_adjp_t1920_r", "I_d_to_I_adjp_t1925_r", "I_d_to_I_adjp_t1930_r")),
  extralines = list(c("$F$-stat", fstat)),
  style.tex = style.tex("aer"), tex = TRUE
)
add_table_row(tablename, "\\multicolumn\\{2\\}\\{c\\}\\{1900\\}", "\\cmidrule(lr){2-3} \\cmidrule(lr){4-5} \\cmidrule(lr){6-7} \\cmidrule(lr){8-9} \\cmidrule(lr){10-11} \\cmidrule(lr){12-13} \\cmidrule(lr){14-15} \\cmidrule(lr){16-17}")
move_estimates_rows(tablename, "^\\s*Constant\\s*&", "\\$F\\$-stat")
edit_table_content(tablename, "& \\$F\\$-stat", "$F$-stat")
cat("Table B44 saved to:", tablename, "\n")
